#include <stdio.h>
#include <string.h>
//#include "getword.h"

#define szMax 512

int getword(char *lbuf, char *buf, size_t size, int first(char c), int rest(char c))
{
  int i = 0;
  char tbuf[szMax];
  strcpy(tbuf, lbuf);
  
  if(*lbuf == '\0') return EOF;
  while(first(tbuf[i]) == 0)
  {
    i++;
  }
  strcpy(&lbuf[0], &tbuf[i]);
  for(i = 0; lbuf[i] != '\0'; i++)
  {
    if(rest(lbuf[i])== 0)
    {
      buf[i]=lbuf[i];
    } else
    {
      break;
    }
  }
  buf[i]='\0';
  
  //fprintf(stderr, "%d\n", i);
  strcpy(lbuf, &lbuf[i]);
  
  //fprintf(stderr, "text = %s, str = %s \n", lbuf, buf);
  if(strlen(buf) < size)
  {
    return strlen(buf);
  } else
  {
    buf[size-1] = '\0';
  }
}
